/**
 * @author Huibin Zheng
 */
function Shape(map)
{
	//this.color = "#FFFFFF";
	this.map = map;
	this.matrixUnit = 0;
	this.keyCoordinations = [];
	this.init = function()
	{
		
	}
	this.getInitPosition = function()
	{
		return Math.floor((this.map.column - this.matrixUnit)/2);
	}
	
	this.down = function()
	{
		return move(Shape.DOWN);
	}
	this.left = function()
	{
		return move(Shape.LEFT);
	}
	this.right = function()
	{
		 return move(Shape.RIGHT);
	}
	this.rotate = function()
	{
		return move(Shape.CLOCK_WISE);
	}
	this.antiRotate = function()
	{
		return move(Shape.ANTI_CLOCK_WISE);
	}
	this.transform = function()
	{
		return true;
	}
	this.quickDrop = function()
	{
		var result = move(Shape.DOWN);
		if(result)
		{
			return this.quickDrop();
		}
		else
		{
			return false;
		}
	}
	
	var self = this;
	function move(direction)
	{
		var nextCoords = mockNextCoordinations(self.keyCoordinations,direction);
		var isValid = validNextCoordinations(nextCoords);
		if(isValid)
		{
			self.keyCoordinations = nextCoords;
			return true;
		}
		else
		{
			if(direction == Shape.DOWN)
			{
				for (var i=0; i < self.keyCoordinations.length; i++) 
				{
					var tempArr = self.keyCoordinations[i];
					for(var j=0;j < tempArr.length ; j++)
					{
						 var coord = tempArr[j];
						 if(coord.value == 1)
						 {
						 	this.map.unitArray[coord.x][coord.y] = 1;
						 }				  		 
					}			 
				}
			}
			return false;
		}
	}
	
	function mockNextCoordinations(currentCoordinations,direction)
	{
		var nextCoordinations = [];
		for (var i=0; i < currentCoordinations.length; i++) 
		{
			var newTempArr = [];
			var tempArr = currentCoordinations[i];
			for (var j=0; j < tempArr.length; j++) 
			{
				  var coord = tempArr[j];
				  switch(direction)
				  {	  	
				  	case Shape.DOWN:
				  		var newCoord = new Coordination(coord.x+1,coord.y,coord.value);
				  		break;
				  	case Shape.LEFT:
				  		var newCoord = new Coordination(coord.x,coord.y-1,coord.value);
				  		break;
				  	case Shape.RIGHT:
				  		var newCoord = new Coordination(coord.x,coord.y+1,coord.value);
				  		break;
				  	case Shape.CLOCK_WISE: 
				  		var value = currentCoordinations[currentCoordinations.length-1-j][i].value;
				  		var newCoord = new Coordination(coord.x,coord.y,value);
				  		break;
				  	case Shape.ANTI_CLOCK_WISE:
				  		var value = currentCoordinations[j][currentCoordinations.length-1-i].value;
				  		var newCoord = new Coordination(coord.x,coord.y,value);
				  		break;
				  		
				  }
				  newTempArr.push(newCoord);
			 }
			 nextCoordinations.push(newTempArr);
		}
		return nextCoordinations;
	}
	
	function validNextCoordinations(nextCoords)
	{
		for (var i=0; i < nextCoords.length; i++) 
		{
			var tempArr = nextCoords[i];
			for (var j=0; j < tempArr.length; j++) 
			{
				  var coord = tempArr[j];
				  if(coord.value == 1)
				  {
					  if(coord.y < 0 || coord.y > self.map.column-1 || coord.x > self.map.row-1)
					  {
					  	return false;
					  }
					  if(self.map.unitArray[coord.x][coord.y] == 1)
					  {
					  	return false;
					  }
				  }
			}

		}
		return true;
	}
}
Shape.LEFT = "left";
Shape.RIGHT = "right";
Shape.DOWN = "down";
Shape.CLOCK_WISE = "clockWise";
Shape.ANTI_CLOCK_WISE = "antiClockWisr";